package com.fruit.service.fruit;

import com.fruit.bean.constant.factory.PageFactory;
import com.fruit.bean.dto.fruit.FruitShelfInfoDto;
import com.fruit.bean.entity.fruit.FruitOnShelfDto;
import com.fruit.bean.entity.fruit.FruitShelfInfo;
import com.fruit.bean.vo.front.Rets;
import com.fruit.bean.vo.query.DynamicSpecifications;
import com.fruit.dao.fruit.FruitShelfRepository;
import com.fruit.dao.fruit.FruitUserOrderRepository;
import com.fruit.service.BaseService;
import com.fruit.utils.factory.Page;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class FruitShelfService extends BaseService<FruitShelfInfo,Long, FruitShelfRepository> {

    @Autowired
    private FruitShelfRepository fruitShelfRepository;

    @Autowired
    private FruitUserOrderRepository userOrderRepository;

    @Autowired
    private FruitStockService stockService;

    @Autowired
    private FruitTypeService typeService;

    /**
     * 上架水果
     * @param stockId 库存id
     * @param sellPrice 出售价格
     * @param sellNumber 出售数量
     * @return
     */
    public Object onShelf(Long stockId, Double sellPrice, Double sellNumber, FruitOnShelfDto fruitOnShelfDto){
        FruitShelfInfo fruitShelfInfo = new FruitShelfInfo();
        fruitShelfInfo.setFruitStockId(stockId);
        fruitShelfInfo.setFruitSellPrice(sellPrice);
        fruitShelfInfo.setFruitSellNumber(sellNumber);
        fruitShelfInfo.setFruitShelfName(fruitOnShelfDto.getShelfName());
        fruitShelfInfo.setFruitShelfDesc(fruitOnShelfDto.getShelfDesc());
        fruitShelfInfo.setFruitShelfImgs(fruitOnShelfDto.getPic());
        fruitShelfInfo.setFruitSellRequestId(fruitOnShelfDto.getSellRequestId());
        fruitShelfRepository.save(fruitShelfInfo);
        return Rets.success();
    }

    /**
     * 普通用户获取商品列表
     * @return
     */
    public List<FruitShelfInfoDto> userQueryShelfList() {
        List<FruitShelfInfoDto> shelfInfoDtoList = new ArrayList<>();
        List<FruitShelfInfo> shelfInfoList = fruitShelfRepository.findAll();
        // 遍历获取需要的信息
        for (FruitShelfInfo fruitShelfInfo : shelfInfoList){
            // 只返回已上架的商品
            if(fruitShelfInfo.getFruitShelfOff() == null || fruitShelfInfo.getFruitShelfOff() == 1){
                continue;
            }
            FruitShelfInfoDto fruitShelfInfoDto = shelfInfoConvert(fruitShelfInfo,0);
            shelfInfoDtoList.add(fruitShelfInfoDto);
        }
        return shelfInfoDtoList;
    }

    /**
     * 获取商品详情
     * @param shelfId
     * @return
     */
    public FruitShelfInfoDto getShelfDetails(Long shelfId){
        FruitShelfInfo fruitShelfInfo = this.get(shelfId);
        FruitShelfInfoDto shelfInfoDto = shelfInfoConvert(fruitShelfInfo,1);
        return shelfInfoDto;
    }

    /**
     * 数据转换，将数据库查询结果转换为前端所需dto
     * @param fruitShelfInfo
     * @param flag 0-列表，只获取第一张图。 1-详情，获取所有图片
     * @return
     */
    private FruitShelfInfoDto shelfInfoConvert(FruitShelfInfo fruitShelfInfo,Integer flag) {
        FruitShelfInfoDto fruitShelfInfoDto = new FruitShelfInfoDto();
        fruitShelfInfoDto.setId(fruitShelfInfo.getId());
        fruitShelfInfoDto.setName(fruitShelfInfo.getFruitShelfName());
        fruitShelfInfoDto.setCharacteristic(fruitShelfInfo.getFruitShelfDesc());
        fruitShelfInfoDto.setPrice(fruitShelfInfo.getFruitSellPrice());
        String fruitPic = fruitShelfInfo.getFruitShelfImgs();
        if(StringUtils.isNotEmpty(fruitPic)){
            if(flag == 0) {
                fruitShelfInfoDto.setPic(fruitPic.split(";")[0]);
            }else{
                fruitShelfInfoDto.setPic(fruitPic);
            }
        }else{
            fruitShelfInfoDto.setPic("");
        }
        Integer orderNumbers = userOrderRepository.countOrderNumberByShelfId(fruitShelfInfo.getId());
        fruitShelfInfoDto.setNumberOrders(orderNumbers);
        fruitShelfInfoDto.setStockNumber(fruitShelfInfo.getFruitSellNumber());
        return fruitShelfInfoDto;
    }

    public Page<FruitShelfInfo> queryShelfPage() {
        Page<FruitShelfInfo> page = new PageFactory().defaultPage();
        Pageable pageable = null;
        if (page.getSort() != null) {
            pageable = PageRequest.of(page.getCurrent() - 1, page.getSize(), page.getSort());
        } else {
            pageable = PageRequest.of(page.getCurrent() - 1, page.getSize(), Sort.Direction.DESC, "id");
        }
        Specification<FruitShelfInfo> specification = DynamicSpecifications.bySearchFilter(
                page.getFilters(), fruitShelfRepository.getDataClass());
        org.springframework.data.domain.Page<FruitShelfInfo> pageResult = fruitShelfRepository.findAll(specification, pageable);
        List<FruitShelfInfo> fruitStockInfoList = pageResult.getContent();
        page.setTotal(Integer.valueOf(pageResult.getTotalElements() + ""));
        page.setRecords(fruitStockInfoList);
        return page;
    }

}
